package com.dd;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

import org.apache.log4j.Logger;

import com.chantel.tool.db.DBUtil;

public class CaiGouGongSi {
	private static final Logger logger = Logger.getLogger(	CaiGouGongSi.class.getName());
	// 公司ID
	private int gongSiId;

	// 采购报表ID
	private int caiGouBiaoId;

	// 公司名称
	private String gongSimingCheng;

	// 联系人方式
	private String lianXirenHeFangshi;

	// 总报价
	private float zongBaoJia;

	// 总审核报价
	private float zongShenHeBaoJia;

	private ArrayList caiGouGongSiBaoJiaXinXis = null;

	public CaiGouGongSi() {
	}

	public CaiGouGongSi(int caiGouBiaoId, String gongSiMingCheng,
			String lianXirenHeFangshi, float zongBaoJia, float zongShenHeJia) {
		super();

		// this.gongSiId = gongSiId;
		this.caiGouBiaoId = caiGouBiaoId;
		this.gongSimingCheng = gongSiMingCheng;
		this.lianXirenHeFangshi = lianXirenHeFangshi;
		this.zongBaoJia = zongBaoJia;
		this.zongShenHeBaoJia = zongShenHeJia;
	}

	public int getCaiGouBiaoId() {
		return caiGouBiaoId;
	}

	public void setCaiGouBiaoId(int caiGouBiaoId) {
		this.caiGouBiaoId = caiGouBiaoId;
	}

	public ArrayList GetCaiGouGongSiBaoJiaXinXis() {
		if (null != this.caiGouGongSiBaoJiaXinXis)
			this.caiGouGongSiBaoJiaXinXis.clear();

		this.caiGouGongSiBaoJiaXinXis = new ArrayList();

		List li = CaiGouGongSiBaoJiaXinXi.GetRecordsFromGongSiId(this.gongSiId);
		Iterator it = li.iterator();

		while (it.hasNext()) {
			Map m = (Map) it.next();

			int id = ((BigDecimal) m.get("Id")).intValue();
			int caiGouShangPingId = ((BigDecimal) m.get("CaiGouShangPingId"))
					.intValue();
			String baoJia = (String) m.get("BaoJia");
			String shenHeJia = (String) m.get("ShenHeJia");

			CaiGouGongSiBaoJiaXinXi cgsp = new CaiGouGongSiBaoJiaXinXi(id,
					caiGouShangPingId, this.gongSiId, baoJia, shenHeJia);

			this.caiGouGongSiBaoJiaXinXis.add(cgsp);
		}

		return this.caiGouGongSiBaoJiaXinXis;
	}

	public void PutData(Map m) {
		m.put("CaiGouBiaoId", new Integer(this.caiGouBiaoId) );
		m.put("GongSimingCheng", this.gongSimingCheng);
		m.put("LianXirenHeFangshi", this.lianXirenHeFangshi);
		m.put("ZongBaoJia", new Float(this.zongBaoJia));
		m.put("ZongShenHeBiaoJia", new Float(this.zongShenHeBaoJia) );
	}

	/**
	 * 执行数据插入 TOD0 error handling 为保证数据完整性，必须在一个事务中处理
	 */
	public boolean InsertGongSi() {
		// TODO 执行数据插入
		DBUtil du = DBUtil.getInstance();
		Map m = new HashMap();

		this.gongSiId = SequenceManager.nextID(SysGlobals.CGGS_ID);
		PutData(m);

		m.put("GongSiId", new Integer(this.gongSiId));

		// do action
		boolean bool_res = du.insertRecord("CaiGouGongSiTable", m);
		return bool_res;
	}

	// 执行数据更新
	/**
	 * 为保证数据完整性，必须在一个事务中处理
	 */
	public boolean UpdateGongSi() {
		// TODO 执行数据更新
		DBUtil du = DBUtil.getInstance();
		Map m = new HashMap();

		PutData(m);

		String updateWhere = "GongSiId=" + this.gongSiId;

		boolean bool_res = du.updateFieldsValue("CaiGouGongSiTable", m,
				updateWhere);

		return bool_res;
	}

	public static List GetRecordsFromCaiGouBiaoId(int caiGouBiaoId) {
		DBUtil du = DBUtil.getInstance();

		String sql = "select* from CaiGouGongSiTable where CaiGouBiaoId ="
				+ caiGouBiaoId;
		logger.debug("GetRecordsFromCaiGouBiaoId():"+sql);
		List l = du.getRecords(sql);
		logger.debug("return size:"+l.size());
		logger.debug("finish GetRecordsFromCaiGouBiaoId()");

		return l;
	}

	// 执行数据删除
	public static boolean DeleteCaiGouGongSiFromCaiGouBiaoId(int caiGouBiaoId) {
		// TODO 执行数据删除
		DBUtil du = DBUtil.getInstance();

		String sql = "delete from CaiGouGongSiTable where CaiGouBiaoId = "
				+ caiGouBiaoId;

		boolean bool_res = du.deleteRecords(sql);

		return bool_res;
	}

	public ArrayList getCaiGouGongSiBaoJiaXinXis() {
		return caiGouGongSiBaoJiaXinXis;
	}

	public void setCaiGouGongSiBaoJiaXinXis(ArrayList caiGouGongSiBaoJiaXinXis) {
		this.caiGouGongSiBaoJiaXinXis = caiGouGongSiBaoJiaXinXis;
	}

	public int getGongSiId() {
		return gongSiId;
	}

	public void setGongSiId(int gongSiId) {
		this.gongSiId = gongSiId;
	}

	public String getGongSimingCheng() {
		return gongSimingCheng;
	}

	public void setGongSimingCheng(String gongSimingCheng) {
		this.gongSimingCheng = gongSimingCheng;
	}

	public String getLianXirenHeFangshi() {
		return lianXirenHeFangshi;
	}

	public void setLianXirenHeFangshi(String lianXirenHeFangshi) {
		this.lianXirenHeFangshi = lianXirenHeFangshi;
	}

	public float getZongBaoJia() {
		return zongBaoJia;
	}

	public void setZongBaoJia(float zongBaoJia) {
		this.zongBaoJia = zongBaoJia;
	}

	public float getZongShenHeBaoJia() {
		return zongShenHeBaoJia;
	}

	public void setZongShenHeBaoJia(float zongShenHeBaoJia) {
		this.zongShenHeBaoJia = zongShenHeBaoJia;
	}

}
